package com.ruoyi.web.controller.iot;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.domain.entity.DeviceThresholdConfig;
import com.ruoyi.iot.service.DeviceThresholdConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 阈值与权重配置接口
 */
@Tag(name = "阈值与权重配置接口")
@RestController
@RequestMapping("/iot/threshold-config")
public class ThresholdConfigController extends BaseController {

    private static final String TAG_THRESHOLD_CONFIG_METRICS = "阈值与权重配置接口";

    private final DeviceThresholdConfigService thresholdConfigService;

    public ThresholdConfigController(DeviceThresholdConfigService thresholdConfigService) {
        this.thresholdConfigService = thresholdConfigService;
    }

    /**
     * 查询阈值配置列表
     *
     * @param query 查询条件
     * @return 阈值配置列表
     */
    @Operation(summary = "查询阈值配置列表", tags = TAG_THRESHOLD_CONFIG_METRICS)
    @GetMapping("/list")
    public TableDataInfo list(DeviceThresholdConfig query) {
        startPage();
        List<?> list = thresholdConfigService.pageWithMetricLabel(query);
        return getDataTable(list);
    }

    /**
     * 新增阈值配置
     *
     * @param config 阈值配置
     * @return 操作结果
     */
    @Operation(summary = "新增阈值配置", tags = TAG_THRESHOLD_CONFIG_METRICS)
    @Log(title = "阈值配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceThresholdConfig config) {
        return toAjax(thresholdConfigService.create(config));
    }

    /**
     * 修改阈值配置
     *
     * @param config 阈值配置
     * @return 操作结果
     */
    @Operation(summary = "修改阈值配置", tags = TAG_THRESHOLD_CONFIG_METRICS)
    @Log(title = "阈值配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceThresholdConfig config) {
        return toAjax(thresholdConfigService.update(config));
    }

    /**
     * 删除阈值配置
     *
     * @param id 主键ID
     * @return 操作结果
     */
    @Operation(summary = "删除阈值配置", tags = TAG_THRESHOLD_CONFIG_METRICS)
    @Log(title = "阈值配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(thresholdConfigService.delete(id));
    }
}
